import { GetShowVerificationStore } from '@/service/login';
import { Action, createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit';
import { HYDRATE } from 'next-redux-wrapper';

export interface TestInitialState {
	num: number;
	showVerification: string;
}

const testSlice = createSlice({
	name: 'test',
	initialState: {
		num: 0,
		showVerification: '关闭'
	} as TestInitialState,
	reducers: {
		increment: (state, action: PayloadAction<number>) => {
			state.num += action.payload;
		},
		decrement: (state, action: PayloadAction<number>) => {
			state.num -= action.payload;
		}
	},
	extraReducers: (builder) => {
		builder
			.addCase(HYDRATE, (state: TestInitialState, action: any) => {
				return {
					...state,
					...action.payload.test
				};
			})
			.addCase(fetchGetShowVerification.fulfilled, (state, { payload }) => {
				state.showVerification = payload;
			});
	}
});

export const fetchGetShowVerification = createAsyncThunk('fetchGetShowVerification', async (params: { channel: string }) => {
	const res = await GetShowVerificationStore(params);
	console.log('res', res);
	return res;
});

export const { increment, decrement } = testSlice.actions;
export default testSlice.reducer;
